package xyz.labmem.app.dialog

import javafx.geometry.Insets
import javafx.scene.control.ScrollPane
import javafx.scene.layout.HBox
import javafx.scene.layout.VBox
import javafx.scene.paint.Color
import javafx.stage.Modality
import javafx.stage.StageStyle
import xyz.labmem.app.config.AppConfig.mouseImage
import xyz.labmem.app.unit.BaseStage
import xyz.labmem.app.unit.LabelTileUnit
import xyz.labmem.app.util.ScreenType


/**
 * @author       : ltty
 * @description  :
 * @createDate   : 2024/10/15
 */
class ConfirmDialog(private val parent: BaseStage) : BaseStage(VBox()) {

    private val title = LabelTileUnit(
        "确认提示",
        22.0,
        color = Color.web("#6464ff"),
        backColor = Color.web("#ff0000"),
        rotateE = 5.0,
        rotateS = .0,
        clearance = 5.0
    )
    private val label = LabelTileUnit(
        "el psy congroo",
        28.0,
        backColor = Color.web("#ff0000"),
        rotateE = 5.0,
        rotateS = .0,
        clearance = 2.0
    )

    private var confirm: () -> Boolean = { println("confirm"); true }

    private var cancel: () -> Unit = { println("cancel"); }

    init {
        initOwner(parent)
        initModality(Modality.WINDOW_MODAL)
        initStyle(StageStyle.TRANSPARENT)
        scene.cursor = mouseImage["m1"]
        scene.fill = null
        root.setPrefSize(440.0, 200.0)
        root.style = """
                       -fx-background-color: black;
                       -fx-border-width: 10;
                       -fx-border-color: linear-gradient(to top, #ffffff, #ff0000); 
                       -fx-padding: 10;
            """.trimIndent()
        title.prefWidth = 400.0
        label.prefWidth = 400.0
        root.children.addAll(title, ScrollPane(label).apply {
            VBox.setMargin(this, Insets(8.0, .0, .0, .0))
            prefHeight = 200.0 - 80.0 - 30.0
        }, HBox().apply {
            VBox.setMargin(this, Insets(10.0))
            children.add(DialogBtn("确定", color = Color.web("#ffed07"), scene = scene) {
                if (confirm()) {
                    close()
                }
            }.apply {
                VBox.setMargin(this, Insets(0.0, 5.0, 20.0, 0.0))
            })
            children.add(DialogBtn("取消", scene = scene) {
                cancel()
                close()
            }.apply {
                VBox.setMargin(this, Insets(0.0, 5.0, 20.0, 0.0))
            })
        })
    }

    fun run(t: String, l: String, confirm: () -> Boolean, cancel: (() -> Unit)? = null) {
        y = parent.y + (ScreenType.getHeight() - root.prefHeight) / 2
        x = parent.x + (ScreenType.getWidth() - root.prefWidth) / 2
        title.textProperty.value = t
        label.textProperty.value = l
        this.confirm = confirm
        cancel?.let { this.cancel = cancel }
        show()
    }
}